home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16428 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  51 lines

  1. Newsgroups: comp.lang.c++
  2. Path: presby.edu!jtbell
  3. From: jtbell@presby.edu (Jon Bell)
  4. Subject: Re: C++ beginner quesion on data member access.
  5. Message-ID: <Dpnpqz.2Io@presby.edu>
  6. Date: Wed, 10 Apr 1996 17:25:46 GMT
  7. References: <4kgb76$r3s@HOPPER.ACM.ORG>
  8. Organization: Presbyterian College, Clinton, South Carolina USA
  9.  
  10.  Ken Varn <varnk@e62.diebold.com> wrote:
  11. > but what do 
  12. >I gain if all I am doing is providing a member function to get the private 
  13. >data or set the private data.  i.e. why call a getData() function that 
  14. >basically just returns the private data member as opposed to just declaring 
  15. >the pviate data member as public.
  16.  
  17. At some point in the future, you might want to change the way the member
  18. data is organized, after which the value in question might not be stored 
  19. directly as a member data item, but can be calculated from other member 
  20. data.  The classic example of this is rectangular versus polar 
  21. representation for complex numbers:
  22.  
  23. class Complex
  24. {
  25. public
  26.    double RealPart (void) const { return RealPart_; };
  27.    double ImagPart (void) const { return ImagPart_; };
  28.    //etc.
  29. private
  30.    double RealPart_, ImagPart_;
  31. };
  32.  
  33. versus
  34.  
  35. class Complex
  36. {
  37. public
  38.    double RealPart (void) const;
  39.    double ImagPart (void) const;
  40.    //etc.
  41. private
  42.    double Magnitude_, PolarAngle_;
  43. };
  44.  
  45. In the second version, the real part and imaginary part can be calculated 
  46. from the magnitude and polar angle.
  47.  
  48. -- 
  49. Jon Bell <jtbell@presby.edu>                        Presbyterian College
  50. Dept. of Physics and Computer Science        Clinton, South Carolina USA
  51.